﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auditoria
{
    public partial class Permisos : Form
    {
        Principal principal2;
        Conexion conn;
        List<String> usuarios = new List<String>();
        List<String> roles = new List<String>();
        List<String> tablespaces = new List<String>();
        List<String> tablas = new List<String>();

        public Permisos(Conexion conexion)
        {
            InitializeComponent();
            this.conn = conexion;
            this.CargarCombo();
            this.iniciarPrivilegios();
        }

        public void getPrincipal2(Principal log)
        {
            this.principal2 = log;
        }

        private void CargarCombo()
        {
            usuarios = conn.todos_Users();
            roles = conn.todos_Roles();
            tablespaces = conn.todos_tablespace();
            cargarUsuarios();
            cargarRoles();
            cargarTablespaces();
        }

        public void cargarUsuarios()
        {
            foreach (String nuevo in usuarios)
            {
                this.cmbUsuarios.Items.Add(nuevo);
            }
        }

        public void cargarRoles(){
            foreach (String nuevo in usuarios){
                this.cmbRoles.Items.Add(nuevo);
            }
        }

        private void iniciarPrivilegios() 
        {
            this.rbt_objprivs.Visible = false;
            this.rbt_sysprivs.Visible = false;
        }

        public void cargarTablespaces(){
            foreach (String nuevo in tablespaces){
                this.cmbTablespaces.Items.Add(nuevo);
            }
        }

        public void cargarTablas() { 
            foreach(String nuevo in tablas){
                this.cmbTablas.Items.Add(nuevo);
            }
            this.cmbTablas.SelectedIndex = 0;
        }

        private void atrasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            this.principal2.Show();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void valida()
        {
            if (RBU.Checked == true)
            {
                cmbUsuarios.Enabled = true;
                cmbRoles.Text = "";
                cmbTablas.Text = "";
                cmbTablespaces.Text = "";
                cmbTablespaces.Enabled = false;
                cmbTablas.Enabled = false;
                cmbRoles.Enabled = false;
            }
            if (RBR.Checked == true)
            {
                cmbRoles.Enabled = true;
                cmbUsuarios.Text = "";
                cmbTablas.Text = "";
                cmbTablespaces.Text = "";
                cmbTablas.Enabled = false;
                cmbUsuarios.Enabled = false;
                cmbTablespaces.Enabled = false;
            }

            if (RBT.Checked == true)
            {
                cmbTablespaces.Enabled = true;
                cmbUsuarios.Text = "";
                cmbRoles.Text = "";
                cmbRoles.Enabled = false;
                cmbUsuarios.Enabled = false;
            }

        }

        private void RBU_CheckedChanged(object sender, EventArgs e)
        {
            valida();
        }

        private void RBR_CheckedChanged(object sender, EventArgs e)
        {
            valida();
        }

        private void RBT_CheckedChanged(object sender, EventArgs e)
        {
            valida();
        }

        private void cmbTablespaces_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!cmbTablas.Enabled)
                cmbTablas.Enabled = true;
            if (this.cmbTablespaces.SelectedIndex == 0)
            {
                this.cmbTablas.Items.Clear();
                this.tablas.Clear();
                this.iniciarPrivilegios();
            }
            else 
            {
                this.iniciarPrivilegios();
                this.cmbTablas.Items.Clear();
                this.tablas.Clear();
                String tablespace_name = this.cmbTablespaces.SelectedItem.ToString();
                tablas = conn.tablas_Por_Tablespace(tablespace_name);
                this.cargarTablas();
            }
        }

        private void cargarPrivilegios() 
        {
            this.rbt_objprivs.Visible = true;
            this.rbt_objprivs.Checked = false;
            this.rbt_sysprivs.Visible = true;
            this.rbt_sysprivs.Checked = false;
        }

        private void cmbUsuarios_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbUsuarios.SelectedIndex == 0)
                this.iniciarPrivilegios();
            else 
            {
                this.cargarPrivilegios();
            }
        }

        private void cmbRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbRoles.SelectedIndex == 0)
                this.iniciarPrivilegios();
            else 
            {
                this.cargarPrivilegios();
            }

        }

        private void rbt_sysprivs_CheckedChanged(object sender, EventArgs e)
        {
            String condicion = "";
            List<String> lista = new List<String>();
            if (this.cmbUsuarios.Enabled)
            {
                condicion = this.cmbUsuarios.SelectedItem.ToString();
                lista = this.conn.SysPrivs_Usr(condicion);
                this.cargarTable2("Usuario", lista);
            }
            else 
            {
                condicion = this.cmbRoles.SelectedItem.ToString();
                lista = this.conn.SysPrivs_Rol(condicion);
                this.cargarTable2("Role", lista);
            }
        }

        private void rbt_objprivs_CheckedChanged(object sender, EventArgs e)
        {
            String condicion = "";
            List<String> lista = new List<String>();
            if (this.cmbUsuarios.Enabled)
            {
                condicion = this.cmbUsuarios.SelectedItem.ToString();
                lista = this.conn.ObjPrivs_Usr(condicion);
                this.cargarTable1("Usuario", lista);
            }
            else
            {
                condicion = this.cmbRoles.SelectedItem.ToString();
                lista = this.conn.ObjPrivs_Rol(condicion);
                this.cargarTable1("Role", lista);
            }
        }

        private void cargarTable1(String grantee, List<String> lista) 
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(grantee);
            dt.Columns.Add("Tablas");
            dt.Columns.Add("Privilegios");
            for (int i = 1; i < lista.Count; i=i+3) 
            {
                DataRow row = dt.NewRow();
                row[grantee] = lista[i];
                row["Tablas"] = lista[i + 1];
                row["Privilegios"] = lista[i + 2];
                dt.Rows.Add(row);
            }
            this.dataGridView1.DataSource = dt;
            this.dataGridView1.Update();
        }

        private void cargarTable2(String grantee, List<String> lista)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(grantee);
            dt.Columns.Add("Privilegios");
            for (int i = 1; i < lista.Count; i = i + 2)
            {
                DataRow row = dt.NewRow();
                row[grantee] = lista[i];
                row["Privilegios"] = lista[i + 1];
                dt.Rows.Add(row);
            }
            this.dataGridView1.DataSource = dt;
            this.dataGridView1.Update();
        }

        private void cmbTablas_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = null;
            if (this.cmbTablas.SelectedIndex == 0) 
                return;
            String table_name = this.cmbTablas.SelectedItem.ToString();
            List<String> lista = new List<String>();
            lista = this.conn.Privs_Tablas(table_name);
            DataTable dt = new DataTable();
            dt.Columns.Add("Tabla");
            dt.Columns.Add("Privilegio");
            dt.Columns.Add("Grantee");
            dt.Columns.Add("Propietario");
            if (lista.Count == 1)
                return;
            for (int i = 1; i < lista.Count; i = i + 4) 
            {
                DataRow row = dt.NewRow();
                row["Tabla"] = lista[i];
                row["Privilegio"] = lista[i+1];
                row["Grantee"] = lista[i+2];
                row["Propietario"] = lista[i+3];
                dt.Rows.Add(row);
            }
            this.dataGridView1.DataSource = dt;
            this.dataGridView1.Update();
        }

    }
}
